package com.team.admin.service.impl;

import com.team.admin.domain.SystemUser;
import com.team.admin.service.AuthenticationJwtService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
@Service
public class AuthenticationJwtServiceImpl implements AuthenticationJwtService {

    private final static String SUBJECT_VALUE = "jwt-token";

    private final static int EXPIRE_TIME = 10 * 60 * 1000;

    private final static String SIGN_KEY = "team-x-sign";

    @Override
    public String createJWT(SystemUser systemUser) {

        String jwt = Jwts.builder()
                .claim("userInfo",systemUser)
                .setSubject(SUBJECT_VALUE)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE_TIME))
                .signWith(SignatureAlgorithm.HS512,SIGN_KEY)
                .compact();
        return jwt;
    }

    @Override
    public SystemUser jwtOfSystemUser(String jwt) {
        Claims claims =
                Jwts.parser().setSigningKey(SIGN_KEY)
                .parseClaimsJws(jwt)
                .getBody();

        SystemUser systemUser = claims.get("userInfo",SystemUser.class);
        return systemUser;
    }
}
